package com.macbulldev.poc.testgin.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EntityManager;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.Transient;
import javax.persistence.Version;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Transactional;


@Configurable
@javax.persistence.Entity
@org.springframework.roo.addon.javabean.RooJavaBean
@org.springframework.roo.addon.tostring.RooToString
@org.springframework.roo.addon.entity.RooEntity
public class Scenario {
	
	private static final Logger LOGGER = Logger.getLogger(Scenario.class);

    private String name;
    
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "scenario")
    private Set<Underwriter> underwriters;
    
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "scenario")
    private Set<TrialGroup> trialGroups;
    
    @javax.persistence.ManyToOne(targetEntity = com.macbulldev.poc.testgin.domain.StateOrProvince.class)
    @javax.persistence.JoinColumn(name="stateOrProvinceId")
    private com.macbulldev.poc.testgin.domain.StateOrProvince stateOrProvince;
    
    @javax.persistence.ManyToOne(targetEntity = com.macbulldev.poc.testgin.domain.PolicyType.class)
    @javax.persistence.JoinColumn(name="policyTypeId")
    private com.macbulldev.poc.testgin.domain.PolicyType policyType;
    
    private String createUsername;
    private String updateUsername;
    private Boolean isApproved;

    @javax.persistence.Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @org.springframework.format.annotation.DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS")
    private java.util.Date createTs;
    
    @javax.persistence.Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @org.springframework.format.annotation.DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS")
    private java.util.Date updateTs;
    
    private String type;
    
    
    public Boolean getIsApproved() {
		return isApproved;
	}

	public void setIsApproved(Boolean isApproved) {
		this.isApproved = isApproved;
	}
	
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
    
    public static List<Scenario> findScenarioByPolicyTypeAbbrName(String abbrName, int firstResult, int maxResults) {    
    	
    	EntityManager entityManager = new Scenario().entityManager;

    	String hql = "select o from Scenario o where upper(trim(o.policyType.abbrName))=upper(trim(?)) order by o.stateOrProvince.displayName asc";

    	return entityManager().createQuery(hql).setParameter(1, abbrName).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }
    
    public static Scenario getByScenarioNameAndStateOrProvince(String name, Long stateId) {    
        
    	 
    	EntityManager entityManager = new Scenario().entityManager;
    	
    	String hql = "select s from Scenario s where upper(trim(s.name))=upper(trim(?)) and s.stateOrProvince.id = ?";
    	
    	
    	Query q = entityManager.createQuery(hql);
    	
    	
    	q.setParameter(1, name);
    	q.setParameter(2, stateId);
    	
    	Scenario retObj = null;
    	
    	try{
    		retObj = (Scenario)q.getSingleResult();
    	}catch(Exception ex)
    	{
    		LOGGER.debug("query error:  "+ex);
    	}
    	
        return retObj;
    }

	@PersistenceContext    
    transient EntityManager entityManager;

	@Id    
    @GeneratedValue(strategy = GenerationType.AUTO)    
    @Column(name = "id")    
    private Long id;

	@Version    
    @Column(name = "version")    
    private Integer version;

	public Long getId() {    
        return this.id;        
    }

	public void setId(Long id) {    
        this.id = id;        
    }

	public Integer getVersion() {    
        return this.version;        
    }

	public void setVersion(Integer version) {    
        this.version = version;        
    }

	@Transactional    
    public void persist() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        this.entityManager.persist(this);        
    }

	@Transactional    
    public void remove() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        if (this.entityManager.contains(this)) {        
            this.entityManager.remove(this);            
        } else {        
            Scenario attached = this.entityManager.find(Scenario.class, this.id);            
            this.entityManager.remove(attached);            
        }        
    }

	@Transactional    
    public void flush() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        this.entityManager.flush();        
    }

	@Transactional    
    public void merge() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        Scenario merged = this.entityManager.merge(this);        
        this.entityManager.flush();        
        this.id = merged.getId();        
    }

	public static final EntityManager entityManager() {    
        EntityManager em = new Scenario().entityManager;        
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");        
        return em;        
    }

	public static long countScenarios() {    
        return (Long) entityManager().createQuery("select count(o) from Scenario o").getSingleResult();        
    }

	public static List<Scenario> findAllScenarios() {    
        return entityManager().createQuery("select o from Scenario o order by o.stateOrProvince.displayName").getResultList();        
    }

	public static Scenario findScenario(Long id) {    
        if (id == null) throw new IllegalArgumentException("An identifier is required to retrieve an instance of Scenario");        
        return entityManager().find(Scenario.class, id);        
    }

	public static List<Scenario> findScenarioEntries(int firstResult, int maxResults) {    
        return entityManager().createQuery("select o from Scenario o order by o.stateOrProvince.displayName").setFirstResult(firstResult).setMaxResults(maxResults).getResultList();        
    }
	public static List<Scenario> findScenarioEntriesByPolicyTypeAbbrName(int firstResult, int maxResults,String abbrName) {    
        return entityManager().createQuery("select o from Scenario o where o.policyType.abbrName = ? order by o.name").setParameter(1, abbrName).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();        
    }
	public static List<Scenario> findScenarioEntriesByPolicyTypeAbbrNameAndState(int firstResult, int maxResults,String abbrName,StateOrProvince sop) {    
        return entityManager().createQuery("select o from Scenario o where o.policyType.abbrName = ? and o.stateOrProvince.id = ? order by o.name").setParameter(1, abbrName).setParameter(2, sop.getId()).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();        
    }

	public String getName() {    
        return this.name;        
    }

	public void setName(String name) {    
        this.name = name;        
    }

	public Set<Underwriter> getUnderwriters() {    
        return this.underwriters;        
    }

	public void setUnderwriters(Set<Underwriter> underwriters) {    
        this.underwriters = underwriters;        
    }

	public List<TrialGroup> getTrialGroups() {
		EntityManager em = new TrialGroup().entityManager;
		return em.createQuery("select o from TrialGroup o where o.scenario.id = ? order by o.id asc").setParameter(1, this.id).getResultList();
        //return this.trialGroups;        
    }

	public void setTrialGroups(Set<TrialGroup> trialGroups) {    
        this.trialGroups = trialGroups;        
    }

	public StateOrProvince getStateOrProvince() {    
        return this.stateOrProvince;        
    }

	public void setStateOrProvince(StateOrProvince stateOrProvince) {    
        this.stateOrProvince = stateOrProvince;        
    }

	public PolicyType getPolicyType() {    
        return this.policyType;        
    }

	public void setPolicyType(PolicyType policyType) {    
        this.policyType = policyType;        
    }

	public String getCreateUsername() {    
        return this.createUsername;        
    }

	public void setCreateUsername(String createUsername) {    
        this.createUsername = createUsername;        
    }

	public String getUpdateUsername() {    
        return this.updateUsername;        
    }

	public void setUpdateUsername(String updateUsername) {    
        this.updateUsername = updateUsername;        
    }

	public Date getCreateTs() {    
        return this.createTs;        
    }

	public void setCreateTs(Date createTs) {    
        this.createTs = createTs;        
    }

	public Date getUpdateTs() {    
        return this.updateTs;        
    }

	public void setUpdateTs(Date updateTs) {    
        this.updateTs = updateTs;        
    }

	public String toString() {    
        StringBuilder sb = new StringBuilder();        
        sb.append("IsApproved: ").append(getIsApproved()).append(", ");        
        sb.append("Id: ").append(getId()).append(", ");        
        sb.append("Version: ").append(getVersion()).append(", ");        
        sb.append("Name: ").append(getName()).append(", ");        
        sb.append("Underwriters: ").append(getUnderwriters() == null ? "null" : getUnderwriters().size()).append(", ");        
        sb.append("TrialGroups: ").append(getTrialGroups() == null ? "null" : getTrialGroups().size()).append(", ");        
        sb.append("StateOrProvince: ").append(getStateOrProvince()).append(", ");        
        sb.append("PolicyType: ").append(getPolicyType()).append(", ");        
        sb.append("CreateUsername: ").append(getCreateUsername()).append(", ");        
        sb.append("UpdateUsername: ").append(getUpdateUsername()).append(", ");        
        sb.append("CreateTs: ").append(getCreateTs()).append(", ");        
        sb.append("UpdateTs: ").append(getUpdateTs());        
        return sb.toString();        
    }
}
